package com.ctl.utils.swing;

import com.alibaba.fastjson.JSON;
import org.apache.kafka.common.protocol.types.Field;
import org.jfree.data.json.impl.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: PointUtil</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2019</p>
 * <p>Company: www.ctl.com</p>
 *
 * @author ctl
 * @version 1.1
 * @date 2022-12-23 14:35
 */
public class PointUtil {
    public static List<Point> getPointsJsonArray(Integer pointNumber, String jsonStr) {
        /**
         * 米每行是一个线段
         * 每个线段一定时平行x或y
         * 线段前后是相连的,首尾点要么x一样要么y一样
         */
        String str = "3481.52,3476.32;3481.52,5168.75\n" +
                "3673.01,5168.75;3673.01,4101.45\n" +
                "3871,4101.45;3871,5357.26\n" +
                "4034.1,5357.26;4034.1,4180.64\n" +
                "4259.89,4180.64;4259.89,5768.94\n" +
                "4450.39,5768.94;4450.39,4167.07\n" +
                "4635.47,4167.07;4635.47,5755.36\n" +
                "4825.97,5755.36;4825.97,4026.79";
        if (jsonStr != null && !"".equals(jsonStr)) {
            str = jsonStr;
        }
        //总共点数
        int pointNum = 60;
        if (pointNumber != null) {
            pointNum = pointNumber;
        }
        String[] split = str.split("\n");
        if (split.length < 2) {
            split = str.replaceAll("\n", "").split("#");
        }
        List<Point> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++) {
            String[] split1 = split[i].split(";");
            for (int j = 0; j < split1.length; j++) {
                String[] split2 = split1[j].split(",");
                Point point = new Point().withX(Double.parseDouble(split2[0])).withY(Double.parseDouble(split2[1]));
                list.add(point);
            }
        }
        int length = list.size();
        //线段的总长度
        Double lucheng = 0d;
        for (int i = 1; i < length; i++) {
            Point pre = list.get(i - 1);
            Point cur = list.get(i);
            if (pre.getX().equals(cur.getX())) {//y方向移动
                lucheng += Math.abs(cur.getY() - pre.getY());
                if (pre.getY() < cur.getY()) {// 向下移动
                } else { //向上移动
                }
            } else if (pre.getY().equals(cur.getY())) {//x方向移动
                lucheng += Math.abs(cur.getX() - pre.getX());
                if (pre.getX() < cur.getX()) {// 向右边移动
                } else { //向左边移动
                }
            } else {
                System.err.println("data error");
            }
        }

        //在上面几个点中均匀的增加多个点,每个点的距离
        Double preLucheng = lucheng / pointNum;
//        System.out.println(lucheng + "\tpointNum=" + pointNum + "\tpreLucheng=" + preLucheng);
//        System.out.println(list);
        List<Point> points = new ArrayList<>();
        for (int i = 1; i < length; i++) {
            Point pre = list.get(i - 1);
            points.add(pre);
            Point cur = list.get(i);
            if (pre.getX().equals(cur.getX())) {//y方向移动
                //当前线段路程
                Double curLucheng = Math.abs(cur.getY() - pre.getY());
                //当前线段所需步数
                int curBuShu = Double.valueOf(curLucheng / preLucheng).intValue();
                Double y = pre.getY();
                for (int j = 0; j < curBuShu; j++) {
                    Point point = new Point().withX(pre.getX());
                    if (pre.getY() < cur.getY()) {// 向下移动
                        y += preLucheng;
                        point.withY(y);
                    } else { //向上移动
                        y -= preLucheng;
                        point.withY(y);
                    }
                    points.add(point);
                }
            } else if (pre.getY().equals(cur.getY())) {//x方向移动
                //当前线段路程
                Double curLucheng = Math.abs(cur.getY() - pre.getY());
                //当前线段所需步数
                int curBuShu = Double.valueOf(curLucheng / preLucheng).intValue();
                Double x = pre.getX();
                for (int j = 0; j < curBuShu; j++) {
                    Point point = new Point().withY(pre.getY());
                    if (pre.getX() < cur.getX()) {// 向右边移动
                        x += preLucheng;
                        point.withX(x);
                    } else { //向左边移动
                        x -= preLucheng;
                        point.withX(x);
                    }
                }
            } else {
                System.err.println("data error");
            }
        }
//        System.out.println(points);
        String returnJson = JSON.toJSONString(points);
        System.out.println(returnJson);
        return points;
    }

    public static List<Point> getPointsJsonArray2(Integer pointNumber, String jsonStr) {
        /**
         * 米每行是一个线段
         * 每个线段一定时平行x或y
         * 线段前后是相连的,首尾点要么x一样要么y一样
         */
        String str = "1736,2341;3628,2341;1893,3366;2532,1782;3163,3382;1736,2341";
        str = "3481.52,3476.32;3481.52,5168.75#3673.01,5168.75;3673.01,4101.45#3871,4101.45;3871,5357.26#4034.1,5357.26;4034.1,4180.64#4259.89,4180.64;4259.89,5768.94#4450.39,5768.94;4450.39,4167.07#4635.47,4167.07;4635.47,5755.36#4825.97,5755.36;4825.97,4026.79";
        if (jsonStr != null && !"".equals(jsonStr)) {
            str = jsonStr;
        }
        //总共点数
        int pointNum = 60;
        if (pointNumber != null) {
            pointNum = pointNumber;
        }
        str = str.replaceAll("#", ";");

        List<Point> list = new ArrayList<>();
        if (str.contains(";")) {
            String[] split = str.split(";");
            for (int i = 0; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                Point point = new Point().withX(Double.parseDouble(split1[0])).withY(Double.parseDouble(split1[1]));
                list.add(point);
            }
        } else {
            //978.934306569343,1939.2700729927,975.466560753592,2891.50613381045,215.210493769959,2905.64000484717,184.625367101961,4944.12672502281,864.477011576186,4961.24615077555,923.450373517336,3316.36091468978,1490.74016309307,3321.77292427007,1482.25404881387,5736.97650547445,2931.55919251825,5813.80702554744,2921.26437043796,5297.3950729927,2262.13458029197,5289.38868613139,2244.6049270073,4098.77737226277,2942.68430656934,4043.10218978102,2927.74817518248,3308.97810218978,2346.05109489051,3322.77372262774,2317.83941605839,2680.29197080292,3349.29927007299,2664.52554744526
            String[] split = str.replaceAll("\\[","").replaceAll("]","").split(",");
            for (int i = 0; i < split.length; i++, i++) {
                Point point = new Point().withX(Double.parseDouble(split[i])).withY(Double.parseDouble(split[i + 1]));
                list.add(point);
            }
        }
        int length = list.size();
        //线段的总长度
        Double lucheng = 0d;
        for (int i = 1; i < length; i++) {
            Point pre = list.get(i - 1);
            Point cur = list.get(i);
            lucheng += distance(pre, cur);

        }

        //在上面几个点中均匀的增加多个点,每个点的距离
        Double preLucheng = lucheng / pointNum;
//        System.out.println(lucheng + "\tpointNum=" + pointNum + "\tpreLucheng=" + preLucheng);
//        System.out.println(list);
        List<Point> points = new ArrayList<>();
        for (int i = 1; i < length; i++) {
            Point pre = list.get(i - 1);
            points.add(pre);
            Point cur = list.get(i);
            if (cur.getX() >= (pre.getX())) {//x方向右边移动
                //当前线段路程
                Double curLucheng = distance(pre, cur);
                //当前线段所需步数
                int curBuShu = Double.valueOf(curLucheng / preLucheng).intValue();
                Double diffX = Math.abs(cur.getX() - pre.getX());
                Double diffY = Math.abs(cur.getY() - pre.getY());
                for (int j = 0; j < curBuShu; j++) {
                    Point point = new Point().withX(pre.getX() + diffX / curBuShu * (j + 1));
                    if (cur.getY() >= pre.getY()) { //y方向上边移动
                        point.withY(pre.getY() + diffY / curBuShu * (j + 1));
                    } else {//y方向下边移动
                        point.withY(pre.getY() - diffY / curBuShu * (j + 1));
                    }
                    points.add(point);
                }
            } else if (cur.getX() <= (cur.getY())) {//x方向左边移动
                //当前线段路程
                Double curLucheng = distance(pre, cur);
                //当前线段所需步数
                int curBuShu = Double.valueOf(curLucheng / preLucheng).intValue();
                Double diffX = Math.abs(cur.getX() - pre.getX());
                Double diffY = Math.abs(cur.getY() - pre.getY());
                for (int j = 0; j < curBuShu; j++) {
                    Point point = new Point().withX(pre.getX() - diffX / curBuShu * (j + 1));
                    if (cur.getY() >= pre.getY()) { //y方向上边移动
                        point.withY(pre.getY() + diffY / curBuShu * (j + 1));
                    } else {//y方向下边移动
                        point.withY(pre.getY() - diffY / curBuShu * (j + 1));
                    }
                    points.add(point);
                }
            }
        }
//        System.out.println(points);
        String returnJson = JSON.toJSONString(points);
        System.out.println(returnJson);
        return points;
    }

    public static Points getPoints(List<Point> pointList, int scale) {
        if (pointList != null) {
            Points points = new Points().withLength(pointList.size());
            for (int i = 0; i < points.getLength(); i++) {
                points.getX()[i] = pointList.get(i).getX().intValue() / scale;
                points.getY()[i] = pointList.get(i).getY().intValue() / scale;
            }
            return points;
        }
        return new Points().withLength(0);
    }

    public static double distance(Point a, Point b) {
        double a1 = a.getX();
        double b1 = a.getY();
        double a2 = b.getX();
        double b2 = b.getY();
        double D = Math.sqrt((a2 - a1) * (a2 - a1) + (b2 - b1) * (b2 - b1));
        return D;
    }

    public static void main(String[] args) {
        List<Point> pointsJsonArray = getPointsJsonArray(120, null);
        Points points = getPoints(pointsJsonArray, 1);
        System.out.println(points);
        String pi = "3481.52,3476.32;3481.52,5168.75#3673.01,5168.75;3673.01,4101.45#3871,4101.45;3871,5357.26";
        pointsJsonArray = getPointsJsonArray(120, pi);
        points = getPoints(pointsJsonArray, 1);
        System.out.println(points);
        pi = "1736,2341;3628,2341;1893,3366;2532,1782;3163,3382;1736,2341";
        getPointsJsonArray2(120, pi);
        System.out.println("3481.52,3476.32;3481.52,5168.75#3673.01,5168.75;3673.01,4101.45#3871,4101.45;3871,5357.26#4034.1,5357.26;4034.1,4180.64#4259.89,4180.64;4259.89,5768.94#4450.39,5768.94;4450.39,4167.07#4635.47,4167.07;4635.47,5755.36#4825.97,5755.36;4825.97,4026.79");
    }


}
